package com.education.platform.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.education.platform.entity.Notification;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Map;

/**
 * 通知Mapper
 *
 * @author education-platform
 * @since 2025-10-27
 */
@Mapper
public interface NotificationMapper extends BaseMapper<Notification> {

    /**
     * 获取通知统计
     */
    @Select("SELECT " +
            "COUNT(*) as total, " +
            "SUM(CASE WHEN status = 'SENT' THEN 1 ELSE 0 END) as sent, " +
            "SUM(CASE WHEN status = 'PENDING' THEN 1 ELSE 0 END) as pending, " +
            "SUM(CASE WHEN DATE(sent_at) = CURDATE() THEN 1 ELSE 0 END) as today_sent " +
            "FROM notification")
    Map<String, Object> getNotificationStats();

    /**
     * 更新发送计数
     */
    @Select("UPDATE notification SET send_count = send_count + #{count}, " +
            "status = 'SENT', sent_at = NOW() WHERE id = #{id}")
    void updateSendCount(@Param("id") Long id, @Param("count") Integer count);

    /**
     * 更新已读计数
     */
    @Select("UPDATE notification SET read_count = read_count + 1 WHERE id = #{id}")
    void updateReadCount(@Param("id") Long id);
}

